package com.gcspring.h2db.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gcspring.h2db.entity.DeviceIdMapper;
import com.gcspring.h2db.repo.DeviceIdMapperRepository;
import com.gcspring.h2db.utils.CodeRuleUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: guc
 * @Description:
 * @Date: 2022-9-1 15:47
 * @Version: 1.0
 */
@Api(value = "设备ID管理", tags = {"设备ID管理"})
@RestController
@RequestMapping("ids")
public class DeviceIdMapperController {
    @Autowired
    private DeviceIdMapperRepository deviceIdMapperRepository;

    @Autowired
    private ObjectMapper objectMapper;

    @ApiOperation("获取所有映射关系")
    @GetMapping("getAll")
    public List<DeviceIdMapper> getAll() {
        return deviceIdMapperRepository.findAll();
    }

    @ApiOperation("通过原小区中Id查询映射信息")
    @GetMapping("findBySrcId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", required = true, value = "类型编码 3位  131为人脸抓拍机，121为车辆抓拍机，220为门禁点，125为门禁设备，126为访客设备，119为在线视频图像信息采集设备", dataTypeClass = String.class),
            @ApiImplicitParam(name = "sbIdSrc", required = true, value = "小区中已有编码", dataTypeClass = String.class)
    })
    public List<DeviceIdMapper> findBySrcId(@RequestParam String type, @RequestParam String sbIdSrc) {
        return deviceIdMapperRepository.findBySrcId(type, sbIdSrc);
    }


    @ApiOperation("添加一个设备id映射")
    @PostMapping("add")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sbIdDest", required = true, value = "20设备编码", paramType = "body"),
            @ApiImplicitParam(name = "sbIdSrc", required = true, value = "小区中已有编码", paramType = "body")

    })
    public String add(@RequestBody DeviceIdMapper idMapper) {
        DeviceIdMapper res = null;
        try {
            res = deviceIdMapperRepository.saveAndFlush(idMapper);
        } catch (Exception e) {
            return "保存异常" + e.getMessage();
        }
        try {
            return objectMapper.writeValueAsString(res);
        } catch (JsonProcessingException e) {
            return "JsonProcessingException";
        }
    }

    @PostMapping("generateDeviceCode")
    @ApiOperation("获取设备ID")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "center", required = true, value = "中心编码 8位  41010268 郑州市公安局中原分局桐柏路派出所", dataTypeClass = String.class),
            @ApiImplicitParam(name = "trade", required = false, value = "行业编码 2位  01  社会治安社区接入，包括社区、楼宇、网吧等", dataTypeClass = String.class),
            @ApiImplicitParam(name = "type", required = true, value = "类型编码 3位  131为人脸抓拍机，121为车辆抓拍机，220为门禁点，125为门禁设备，126为访客设备，119为在线视频图像信息采集设备", dataTypeClass = String.class),
            @ApiImplicitParam(name = "net", required = false, value = "网络标识 1位  其中0、1、2、3、4为监控报警专网，5为公安信息网，6为政务网，7为Internet网，8为社会资源接入网，9为预留", dataTypeClass = String.class),
            @ApiImplicitParam(name = "series", required = true, value = "序列号  6位  0-999999", dataTypeClass = String.class)
    })
    public String generateDeviceCode(@RequestParam String center, @RequestParam(required = false, defaultValue = "01") String trade, @RequestParam String type, @RequestParam(required = false, defaultValue = "7") String net, @RequestParam Integer series) {
        return CodeRuleUtils.getDeviceId(center, trade, type, net, series);
    }
}
